package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.ChuKuDao;
import com.aaa.qy127.entity.ChuKu;
import com.aaa.qy127.service.ChuKuService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service("chuKuServiceImpl")
public class ChuKuServiceImpl implements ChuKuService {

    @Resource(name = "chuKuDaoImpl")
    private ChuKuDao chuKuDao;

    /**
     * 查询 所有的出库信息并分页 显示
     * @param chuKu
     * @return
     */
    @Override
    public Result getAllCK(ChuKu chuKu){
        List<Map<String, Object>> allCK = chuKuDao.getAllCK(chuKu);
        int count = chuKuDao.getAllCount(chuKu);
        Result result = new Result(count, allCK);
        return result;
    }

    /**
     * 查询所有的出库详情信息， 并分页
     * @param chuKu
     * @return
     */
    @Override
    public Result getAllChuKu(ChuKu chuKu){
        List<Map<String, Object>> allChuKu = chuKuDao.getAllChuKu(chuKu);
        int allChuKuCount = chuKuDao.getAllChuKuCount(chuKu);
        Result result = new Result(allChuKuCount, allChuKu);
        return result;
    }

    /**
     * 插入一条 出库信息 并 把 出库详情 添加进去
     * @param totalprice
     * @param totalcount
     * @param jsr
     * @param chukuxinxi
     * @param request
     * @return
     */
    @Override
    public CodeAndMsg addChuKu(String totalprice, String totalcount, String jsr, String chukuxinxi, HttpServletRequest request){

        //添加 出库信息 并返回 主键值
        int i = chuKuDao.insertChuKu(totalprice, totalcount, jsr, request);
        //System.out.println(i);
        //添加 出库详情信息
        chuKuDao.insertAllChuku(i,chukuxinxi);

        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (i>0){
            codeAndMsg.setCode(1001);
            codeAndMsg.setMsg("添加成功");
        }else {
            codeAndMsg.setCode(1001);
            codeAndMsg.setMsg("添加成功");
        }
        return codeAndMsg;
    }

}
